<template>
  <div>
   <el-cascader @change="changeaddress" :props="{ checkStrictly: true,multiple:true }" collapse-tags ref="cascader" clearable v-model="code" :options="options" :placeholder="name" :style="{width:this.w || '250px'}"></el-cascader>
  </div>
</template>

<script>
    import {tongyidizhi} from '../../../assets/tongyidiqu.js'
  export default {
    name: 'tongyidizhi',
    props:['cengshu','sheng','shi','qu','name','w','transfer_area_code'],
    data() {
      return {
        options:"",
        code:[],

        sheng:"",
        shi:"",
        qu:"",
        shengcode:"",
        shicode:"",
        qucode:"",

        cs:1
      }
    },
    created() {

      if(this.cengshu){
        this.cs = this.cengshu
      }
      //两层地址
      if(this.cs == 1){
        tongyidizhi.forEach(item=>{
          item.children = ""
        })
      }
      if(this.cs == 2){
        tongyidizhi.forEach(item=>{
          if(item.children){
            item.children.forEach(item2=>{
              item2.children = ""
            })
          }
        })
      }
      if(this.cs == 3){
        tongyidizhi.forEach(item=>{
          if(item.children){
            item.children.forEach(item2=>{

              if(item2.children){
                item2.children.forEach(item3=>{
                  item3.children = ""
                })
              }
            })
          }
        })
      }

      this.options = tongyidizhi


      if(this.transfer_area_code){
        this.code = this.transfer_area_code
      }else{
        this.chushihua()
      }

    },

    methods: {
      chushihua:function(){

        this.options.forEach((item1,index)=>{
          if(item1.label == this.sheng){
            this.code[0] = item1.value
          }
          if(this.cs == 2 || this.cs == 3){
            item1.children.forEach(item2=>{
              if(item2.label == this.shi){
                this.code[1] = item2.value
              }
              if(this.cs == 3){
                item2.children.forEach(item3=>{
                  if(item3.label == this.qu){
                    this.code[2] = item3.value
                  }
                })
              }
            })
          }
        })

      },
      changeaddress:function(val){


        this.$nextTick(()=>{
          console.log('val')
          console.log(val)
          console.log('getCheckedNodes')
          console.log(this.$refs.cascader.getCheckedNodes())
          let arr = []

            if(val != ''){
              this.$refs.cascader.getCheckedNodes().forEach((item)=>{
                let obj = {
                  "p": "",
                  "c": ""
                }
                if(item.parent){
                  obj.p = item.parent.label
                  obj.c = item.label
                }else{
                  obj.p = item.label
                }
                arr.push(obj)
              })
              this.transfer_area = arr
            }else{
              this.sheng = ""
              this.shi = ""
              this.qu = ""
              this.shengcode = ""
              this.shicode = ""
              this.qucode = ""

              this.transfer_area = []
            }


          console.log('transfer_area')
          console.log(this.transfer_area)

          let obj = {
            sheng:this.sheng,
            shi:this.shi,
            qu:this.qu,
            shengcode:this.shengcode,
            shicode:this.shicode,
            qucode:this.qucode,

            transfer_area:this.transfer_area,
            transfer_area_code:this.code
          }
          this.$emit('chuandizhi',obj)
        })

      }
    }
  }
</script>

<!--

shhhh
12312312312
samguo
chihihuh -->
<style scoped>
.t_age_a_box{
  display: flex;
  align-items: center;
}

/deep/ .t_age_a_box .el-input{
  width: 150px !important;
  min-width: auto !important;
}
/deep/.t_age_a_box .el-input .el-input__inner{
  width: 150px !important;
}
</style>
<style lang="scss" scoped>
  .index {
    /deep/ .el-select {
      min-width: 250px;
      max-width: 100%;
      margin-right: 20px;
    }

    /deep/ .el-input {
      min-width: 250px;
      max-width: 100%;
      margin-right: 20px;
    }

    /deep/ .el-input__inner {
      line-height: 36px;
      height: 36px;
      font-size: 12px;
      background: rgb(247, 248, 250);
      border: 0;
    }

    /deep/ .el-range-editor .el-range-input {
      background: #f7f8fa;
    }

    /deep/ .el-button {
      padding: 11px 23px !important;
    }

    /deep/ .el-button--text {
      padding: 11px 0 !important;
    }

    /deep/ .el-button--danger {
      color: #FFF;
      background-color: #2974FF;
      border-color: #2974FF;
    }

    /deep/ .el-input__inner:-moz-placeholder {
      color: rgb(140, 140, 140);
    }

    /deep/ .el-input__inner:-ms-input-placeholder {
      color: rgb(140, 140, 140);
    }

    /deep/ .el-input__inner::-webkit-input-placeholder {
      color: rgb(140, 140, 140);
    }

    /deep/ .td2 .el-button {
      padding: 11px 0px !important;
    }

    /deep/ .pagination-container .el-select {
      width: 120px;
      margin-right: 20px;
    }

    /deep/ .pagination-container .el-input {
      width: 120px;
      margin-right: 20px;
    }

    /deep/ .pagination-container .el-input__inner {
      line-height: 28px;
      height: 28px;
    }

    /deep/ .el-pagination.is-background .btn-prev:disabled {
      background: rgba(246, 246, 246, 0.3);
      border: 1px rgba(167, 167, 167, 0.3) solid;
    }

    /deep/ .el-pagination.is-background .btn-next:disabled {
      background: rgba(246, 246, 246, 0.3);
      border: 1px rgba(167, 167, 167, 0.3) solid;
    }

    /deep/ .el-pagination.is-background .btn-next {
      background: rgba(246, 246, 246, 1);
      border: 1px rgba(167, 167, 167, 1) solid;
    }

    /deep/ .el-pagination.is-background .btn-prev {
      background: rgba(246, 246, 246, 1);
      border: 1px rgba(167, 167, 167, 1) solid;
    }

    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active {
      background-color: #2974FF;
    }

    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active:hover {
      color: #fff;
    }

    /deep/ .el-pagination.is-background .el-pager li:not(.disabled):hover {
      color: #2974FF;
    }

    /deep/ .el-pager li {
      border-radius: 14px;
      background: rgba(255, 255, 255, 1);
      border: 1px rgba(167, 167, 167, 0.3) solid;
    }

    /deep/ .btn-prev {
      border-radius: 14px;
      padding: 0 10px;
    }

    /deep/ .btn-next {
      border-radius: 14px;
      padding: 0 10px;
    }

    .t_add_cont {
      padding: 30px 10px;
    }

    .t_tips_box {
      background-color: #EEF7FF;
      display: flex;
      justify-content: space-between;
      font-size: 14px;
      padding: 20px;
      color: #7D7D7D;
      margin-bottom: 20px;
    }

    .t_chatkf {
      color: #237BFF;
      margin-left: 20px;
      cursor: pointer;
    }

    .back {
      color: black;
      margin-bottom: 15px;
      display: block;
    }

    .t_btn_box {
      display: flex;
      justify-content: end;
    }

    .see {
      color: #409EFF;
      margin: 20px 0;
      cursor: pointer;
    }

    .cpflex {
      display: flex;
      flex-wrap: wrap;

      .el-form-item {
        margin-bottom: 0px;
      }
    }

    .kucun {
      /deep/ .el-input {
        width: 200px;
      }

      .list {
        display: flex;
        text-align: center;
        border-bottom: 1px #EAEAEA solid;

        .bor1 {
          border-right: 1px #EAEAEA solid;

          i {
            color: #409EFF;
            font-size: 14px;
            margin-left: 10px;
            cursor: pointer;
          }
        }

        .th {
          background: #F9F9F9;
          line-height: 45px;
          flex: 1;
          font-size: 14px;
          box-sizing: border-box;
        }

        .td {
          line-height: 45px;
          font-size: 14px;
          flex: 1;
          box-sizing: border-box;
          color: #666;
        }

        .td2 {
          flex: 1.5
        }
      }
    }



    .o_ul {
      background: #F9F9F9;
      display: flex;

      .o_li {
        flex-basis: 160px;
        line-height: 45px;
        display: block;
        color: #727272;
        border-top: 3px #F9F9F9 solid;
        font-size: 14px;
        text-align: center;
      }

      .act {
        border-top: 3px #DD2727 solid;
        background: #FFFFFF;
      }
    }

    .title {
      border-bottom: 1px #EAEAEA solid;
      line-height: 45px;
      padding-left: 30px;
      font-size: 16px;
      background: #F9F9F9;
      color: #727272
    }

    .bsm {
      padding: 20px 20px 0 20px;

      .el-input {
        width: 220px;
      }
    }

    .row2 {
      color: #FFF;
      margin-top: 20px;

      a {
        color: #FFF;
      }
    }

    .con {
      margin: 20px;
      border: 1px #EAEAEA solid;

      .list {
        display: flex;
        text-align: center;
        border-bottom: 1px #EAEAEA solid;

        .bor1 {
          border-right: 1px #EAEAEA solid;

          i {
            color: #409EFF;
            font-size: 14px;
            margin-left: 10px;
            cursor: pointer;
          }
        }

        .th {
          background: #F9F9F9;
          line-height: 45px;
          flex: 1;
          font-size: 14px;
          box-sizing: border-box;
        }

        .td {
          line-height: 45px;
          font-size: 14px;
          flex: 1;
          box-sizing: border-box;
          color: #666;
        }

        .td2 {
          flex: 1.5
        }
      }
    }

    .el-pagination {
      text-align: right;
      margin-right: 20px;
      padding-bottom: 50px;
    }


  }
</style>
